package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ClassInstructorTeacherMapper;
import com.zhentao.mapper.ClassesMapper;
import com.zhentao.mapper.InstructorMapper;
import com.zhentao.mapper.TeacherMapper;
import com.zhentao.pojo.ClassInstructorTeacher;
import com.zhentao.pojo.Classes;
import com.zhentao.pojo.Instructor;
import com.zhentao.pojo.Teacher;
import com.zhentao.service.InstructorService;
import com.zhentao.vo.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author hp
* @description 针对表【instructor】的数据库操作Service实现
* @createDate 2025-07-09 15:28:47
*/
@Service
public class InstructorServiceImpl extends ServiceImpl<InstructorMapper, Instructor>
    implements InstructorService{

    @Autowired
    private ClassInstructorTeacherMapper classInstructorTeacherMapper;

    @Autowired
    private ClassesMapper classesMapper;

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public Page<Instructor> pageInstructorWithClassesAndTeachers(Query query) {
        // 创建分页对象
        Page<Instructor> page = new Page<>(query.getCurrent(), query.getSize());

        // 构建查询条件
        LambdaQueryWrapper<Instructor> wrapper = new LambdaQueryWrapper<>();

        // 按导员姓名模糊查询
        if (StringUtils.hasText(query.getInstructorName())) {
            wrapper.like(Instructor::getName, query.getInstructorName());
        }

        // 按性别查询
        if (query.getSex() != null) {
            wrapper.eq(Instructor::getSex, query.getSex());
        }

        // 执行分页查询
        Page<Instructor> instructorPage = this.page(page, wrapper);

        // 为每个导员查询班级和讲师信息
        for (Instructor instructor : instructorPage.getRecords()) {
            // 1. 查询该导员所带班级的关联关系
            List<ClassInstructorTeacher> relations = classInstructorTeacherMapper
                .selectList(new QueryWrapper<ClassInstructorTeacher>()
                    .eq("instructor_id", instructor.getId()));

            // 2. 提取班级ID列表
            List<Integer> classIds = relations.stream()
                .map(ClassInstructorTeacher::getClassId)
                .collect(Collectors.toList());

            // 3. 查询班级信息
            List<Classes> classList = new ArrayList<>();
            if (!classIds.isEmpty()) {
                classList = classesMapper.selectBatchIds(classIds);
            }

            // 4. 为每个班级查询对应的讲师信息
            for (Classes classes : classList) {
                // 找到该班级对应的关联关系
                ClassInstructorTeacher relation = relations.stream()
                    .filter(r -> r.getClassId().equals(classes.getId()))
                    .findFirst()
                    .orElse(null);

            }

            // 5. 设置导员的班级列表
            instructor.setClasses(classList);

            // 6. 查询搭班讲师信息（该导员所带班级的所有讲师）
            List<Teacher> partnerTeachers = new ArrayList<>();
            if (!classIds.isEmpty()) {
                // 查询所有与该导员搭班的讲师ID（去重）
                List<Integer> teacherIds = relations.stream()
                    .map(ClassInstructorTeacher::getTeacherId)
                    .distinct()
                    .collect(Collectors.toList());

                if (!teacherIds.isEmpty()) {
                    partnerTeachers = teacherMapper.selectBatchIds(teacherIds);
                }
            }
            instructor.setPartnerTeachers(partnerTeachers);
        }

        return instructorPage;
    }

    @Override
    public Instructor getInstructorWithClassesAndTeachers(Integer instructorId) {
        // 查询导员基本信息
        Instructor instructor = this.getById(instructorId);
        if (instructor == null) {
            return null;
        }

        // 查询该导员所带班级的关联关系
        List<ClassInstructorTeacher> relations = classInstructorTeacherMapper
            .selectList(new QueryWrapper<ClassInstructorTeacher>()
                .eq("instructor_id", instructorId));

        // 提取班级ID列表
        List<Integer> classIds = relations.stream()
            .map(ClassInstructorTeacher::getClassId)
            .collect(Collectors.toList());

        // 查询班级信息
        List<Classes> classList = new ArrayList<>();
        if (!classIds.isEmpty()) {
            classList = classesMapper.selectBatchIds(classIds);
        }

        // 为每个班级查询对应的讲师信息
        for (Classes classes : classList) {
            // 找到该班级对应的关联关系
            ClassInstructorTeacher relation = relations.stream()
                .filter(r -> r.getClassId().equals(classes.getId()))
                .findFirst()
                .orElse(null);


        }

        // 设置导员的班级列表
        instructor.setClasses(classList);

        // 查询搭班讲师信息（该导员所带班级的所有讲师）
        List<Teacher> partnerTeachers = new ArrayList<>();
        if (!classIds.isEmpty()) {
            // 查询所有与该导员搭班的讲师ID（去重）
            List<Integer> teacherIds = relations.stream()
                .map(ClassInstructorTeacher::getTeacherId)
                .distinct()
                .collect(Collectors.toList());

            if (!teacherIds.isEmpty()) {
                partnerTeachers = teacherMapper.selectBatchIds(teacherIds);
            }
        }
        instructor.setPartnerTeachers(partnerTeachers);

        return instructor;
    }

}




